<%@page import="jpa.entities.Employee"%>
<%@page import="jpa.entities.Project"%>
<%@page import="jsp.Datetime"%>
<jsp:useBean id="user" class="jsp.classes.Session" scope="session"/>
<%
    String n = request.getParameter("pid");
    Project p = null;
    if (!(n == null || n.equals(""))) {
        p = user.findProjectById(Long.parseLong(request.getParameter("pid")));
    }
%>

<script type="text/javascript">
    function submitForm()
    {   
        var stateMessage = "";
        
        var data = $("#projectForm").serialize();
        
        $.ajax({
            type: "post",
            url: "http://localhost:8080/Baalzebub/editproject",
            data: data,
            success: function (data, textStatus, jqXHR) {
                if(data == "true")
                {
                    $("#state").html("The project is updated.");
                    
                }
                else
                {
                    $("#state").html("An error has occurred");
                }
            },
            error: function () {
                $("#state").html("Please fill in all the required fields!");
            }
        });
    }
</script>

<form method="post" action="" name="profile" id="projectForm" target="_blank">
    <table>
        <tbody>
            <tr><td colspan="2"><p style="margin:0;padding:0;" id="state"></p></td></tr>
            <tr>
                <td>Project Name:</td>
                <td>
                    <input class="inputField" type="text" name="projectName" id="projectName" value="<%=p.getTitle()%>"/>
                </td>
            </tr>
            <tr>
                <td>Leader:</td>
                <td>
                    <select name="projectLeader" id="testje">
                        <%
                            String selected = "";
                            for (Employee emp : user.getJPAConnection().getAllUsers()) {
                                selected = "";
                                if (p.getProjectLeader() == emp) {
                                    selected = "selected='selected'";
                                }

                                out.print("<option value=\"" + emp.getId() + "\"  " + selected + ">" + emp.getUserName() + "</option>");
                            }
                        %>

                    </select>
                </td>
            </tr>
            <tr>
                <td>Start Date:</td>
                <td>
                    <input class="inputField" type="text" name="projectDate" id="projectDate" value="<%= Datetime.format(p.getStartingDate(), "%d-%m-%Y")%>"/>
                </td>
            </tr>
            <tr>
                <td>Version:</td>
                <td>
                    <input class="inputField" type="text" name="projectVersion" id="projectVersion" value="<%=p.getVersion()%>"/>
                </td>
            </tr>
            <tr>
                <td>Finished?:</td>
                <td>
                    <%
                        String fin = "";
                        if (p.isFinished()) {
                            fin = "checked";
                        }
                    %>
                    <input class="inputField" type="checkbox" name="projectState" <%= fin%> id="projectState"/>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <a href="javascript: submitForm()" class="saveButton"><div class="saveButton"><p class="okText">SAVE</p></div></a>
                </td>
            </tr>
        </tbody>
    </table>
    <input type="hidden" value="<%= p.getId()%>" name="projectId" />
</form>